#!/usr/bin/python
#
# Copyright (c) 2008, Mathias Weber
# Copyright (c) 2008, Thomas Stauffer
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id: example-dialog.py 82 2008-10-12 22:28:21Z tymayos $

import sys
import random

from pandac.PandaModules import TextNode
from pandac.PandaModules import TextProperties
from pandac.PandaModules import TextPropertiesManager

import direct.directbase.DirectStart

base.setBackgroundColor(0.0, 0.0, 0.2)
base.disableMouse()

camera.setPos(0.0, -10.0, 5.0)
camera.lookAt(0.0, 0.0, 0.0)

manX, manY = -1.0, 0.0
womanX, womanY = 1.0, 0.0

man = loader.loadModel("man")
man.reparentTo(render)

woman = loader.loadModel("woman")
woman.reparentTo(render)

def moveMan(x, y):
    global manX, manY
    manX += x
    manY += y
    man.setPos(manX, manY, 0.0)

def moveWoman(x, y):
    global womanX, womanY
    womanX += x
    womanY += y
    woman.setPos(womanX, womanY, 0.0)

moveMan(0.0, 0.0)
moveWoman(0.0, 0.0)

"""
Text nodes.
"""

BORDER = 0.5

texurePaper = loader.loadTexture("paper.png")

textMan = TextNode("Text")
textMan.setText("-")
textMan.setAlign(TextNode.ACenter)
textMan.setTextColor(0.0, 0.0, 0.0, 1.0)
textMan.setCardTexture(texurePaper)
textMan.setCardAsMargin(BORDER, BORDER, BORDER, BORDER)
textMan.setCardDecal(True)
textMan.setShadowColor(1.0, 1.0, 1.0, 1.0)
textMan.setShadow(0.05, 0.05)
textManNode = man.attachNewNode(textMan)
textManNode.setScale(0.2)
textManNode.setPos(0.0, 0.0, 2.5)
textManNode.setBillboardPointEye()

textWoman = TextNode("Text")
textWoman.setText("-")
textWoman.setAlign(TextNode.ACenter)
textWoman.setTextColor(0.0, 0.0, 0.0, 1.0)
textWoman.setCardTexture(texurePaper)
textWoman.setCardAsMargin(BORDER, BORDER, BORDER, BORDER)
textWoman.setCardDecal(True)
textWoman.setShadowColor(1.0, 1.0, 1.0, 1.0)
textWoman.setShadow(0.05, 0.05)
textWomanNode = woman.attachNewNode(textWoman)
textWomanNode.setScale(0.2)
textWomanNode.setPos(0.0, 0.0, 2.5)
textWomanNode.setBillboardPointEye()

propertySelected = TextProperties()
propertySelected.setTextColor(1.0, 0, 0, 1.0)
propertyManager = TextPropertiesManager.getGlobalPtr()
propertyManager.setProperties("selected", propertySelected)

"""
Woman chat interface. Show only a random text.
"""

womanSentences = ["Hi.", "Hello.", "*Giggles*"]
womanSelection = None

def womanTalk():
    textWoman.setText(womanSelection)

def womanQuestion():
    global womanSelection
    womanSelection = random.choice(womanSentences)
    womanTalk()

womanQuestion()

"""
Man chat interface. Possibility to chose an answer.
"""

manSentences = [["Hi.", "Ho.", "He."], ["Whas Up?", "What?"]]
manSelection, manSelectionIndex = None, 0

def manTalk():
    message = ""
    for i in range(len(manSelection)):
        if i == (manSelectionIndex % len(manSelection)):
            message += "\1selected\1" + manSelection[i] + "\2\n"
        else:
            message += manSelection[i] + "\n"

    textMan.setText(message)

def manQuestion():
    global manSelection, manSelectionIndex
    manSelection = random.choice(manSentences)
    manSelectionIndex = 0
    manTalk()

def manSelect(direction):
    global manSelectionIndex
    manSelectionIndex += direction
    manTalk()

manQuestion()

"""
Keyboard.
"""

base.accept("escape", sys.exit)
base.accept("a", render.analyze)
base.accept("o", base.oobe)

base.accept("1", manQuestion)
base.accept("2", womanQuestion)

base.accept("arrow_up", manSelect, [-1])
base.accept("arrow_down", manSelect, [1])

"""
 t
fgh
"""

base.accept("h", moveMan, [0.1, 0.0])
base.accept("f", moveMan, [-0.1, 0.0])
base.accept("t", moveMan, [0.0, 0.1])
base.accept("g", moveMan, [0.0, -0.1])

"""
 i
jkl
"""

base.accept("l", moveWoman, [0.1, 0.0])
base.accept("j", moveWoman, [-0.1, 0.0])
base.accept("i", moveWoman, [0.0, 0.1])
base.accept("k", moveWoman, [0.0, -0.1])

run()
